<?php

require_once ("log.php");
error_reporting(E_ERROR);
require_once ("cloud_api.php");

class cloud_group {
	// 进入主界面 
	function my_group() {
		global $v_file;
		require($v_file);
	}

	// 获取数据
	function get_group_data() {
		// 将数字转换成时间格式
		function stf($value) {
			if ($value < 10) {
				return "0{$value}";
			} else if ($value % 10 == 0) {
				$value = $value/10;
				return "{$value}0";
			} else {
				return $value;
			}
		}
		// 将数值转换为时间表示
		function chang_value_time($value) {
			if ($value < 60) {
				$value = stf($value);
				return "00:{$value}";
			} elseif ($value < 3600) {
				$hour = stf(floor($value/60));
				$min = stf($value%60);

				return "{$hour}:{$min}";
			} else {
				return "1天";
			}
		}

		// 获取开始和结束时间列表
		function get_time_list($select_time) {
			$time = 0;
			while (1) {
				if ($time > 1440) {
					break;
				}
				$c_date = date("i:s", $time);
				if ($select_time == $c_date) {
					$group_list .= "<option value = {$c_date} selected = 'selected'>{$c_date}</option>";
				} else {
					$group_list .= "<option value = {$c_date}>{$c_date}</option>";
				}
				$time += 10;
			}
			return $group_list;
		}

		// 获取发单状态
		function get_wait_list($select_time, $start_time = 30, $end_time = 3600) {
			$time = $start_time;
			while (1) {
				if ($time > $end_time) break;

				if ($select_time == $time) {
					$group_list .= "<option value = {$time} selected = 'selected' >".chang_value_time($time)."</option>";
				} else {
					$group_list .= "<option value = {$time}>".chang_value_time($time)."</option>";
				}
				if ($time < 60) {
					$time += 5;
				} else if ($time < 120) {
					$time += 10;
				} else if ($time < 360) {
					$time += 20;
				} else if ($time < 720) {
					$time += 30;
				} else {
					$time += 60;
				}
			}
			return $group_list;
		}
		
		// 获取商品列表
		function get_good_list($g_good_type) {
			$goodlist = array();
			for ($i = 0; $i < count($g_good_type); ++$i) {
				if ($g_good_type[$i] != "") {
					$goodlist[$g_good_type[$i]] = true;
				}
			}

			// 获取所有的商品列表
			$index = 1;
			$onlyone = array();
			$category = AllQuery("SELECT * FROM parent_category");
			$group_list = '';
			for ($i = 0; $i < count($category); ++$i) {
				$row 				= $category[$i];
				$id 				= $row['id'];
				$search_expression 	= $row['search_expression'];
				$name 				= $row['name'];
				$status				= $row['status'];
				$image 				= $row['image'];
				if (!$onlyone[$id] && $status == 1) {
					$onlyone[$id] = true;
					$num = 3;
					if ($index % $num == 1) {
						$group_list .= "<li data-id='82400'>";
						if ($index == 1) {
							$group_list .= "<input class='li-input2' placeholder = '' readonly = 'readonly' type = 'text' value = '商品列表：' >";
						} else {
							$group_list .= "<input class='li-input2' placeholder = '' readonly = 'readonly' type = 'text' value = '' >";
						}
					}

					// 判断是否需要勾选
					if ($goodlist[$id]) {
						$group_list .= "<input class = 'li-input6' placeholder = '' ip = 'good_type{$index}' name = '{$name}' checked = 'checked' type = 'checkbox'  value = {$id}>";
					} else {
						$group_list .= "<input class = 'li-input6' placeholder = '' ip = 'good_type{$index}' name = '{$name}' type = 'checkbox'  value = {$id}>";
					}

					$group_list .= "<input class = 'li-input7' placeholder = '' readonly = 'readonly' type = 'text' value = {$name}>";
					if ($index % $num == 0) {
						$group_list .= "</li>";
					}
					$index += 1;
				}
			}
			return $group_list;
		}

		$serial = $_COOKIE['g_serial'];
		$group_list = "";
		$row = simQuery("SELECT * FROM wechat_group WHERE id = {$serial}");
		if ($row) {
			$g_serial				= $row['id'];					// 唯一码
			$g_from_name	 		= $row['wechat_room_name'];		// 群名
			$g_final_from_wxid 		= $row['final_from_wxid'];		// 微信号
			$g_taobao_ance			= $row['taobao_ance'];			// 淘宝ance
			$g_pdd_ance				= $row['pdd_ance'];				// 拼多多ance
			$g_jd_ance				= $row['jd_ance'];				// 京东ance
			$g_final_from_name 		= $row['final_from_name'];		// 姓名
			$g_invite_code 			= $row['invite_code'];			// 邀请码
			$g_start_time 			= $row['start_time'];			// 群发单开始时间
			$g_finish_time	 		= $row['finish_time'];			// 群发单结束时间
			$g_with_preferred		= $row['with_preferred'];		// 是否选择优选库
			$g_bot_statue 			= $row['status'];				// 启动or关闭
			$g_interval 			= $row['interval'];				// 等待间隔
			$g_lunch_break			= $row['lunch_break'];			// 午休间隔
			$parent_category_ids 	= $row['parent_category_ids'];	// 一键跟发

			$group_list .= "<li data-id='82400'>";
			$group_list .= "<input class='li-input2' placeholder = '' readonly = 'readonly' type = 'text' value = '群名 : ' >";
			$group_list .= "<input class='li-input3' placeholder = '' readonly = 'readonly' type = 'text' value = {$g_from_name}>";
			$group_list .= "</li>";

			$group_list .= "<li data-id = '82400'>";
			$group_list .= "<input class ='li-input2' placeholder = '' readonly = 'readonly' type = 'text' value = '邀请码 : ' >";
			$group_list .= "<input class ='li-input3' placeholder = '' readonly = 'readonly' type = 'text' value = {$g_invite_code}>";
			$group_list .= "</li>";

			$group_list .= "<li data-id = '82400'>";
			$group_list .= "<input class = 'li-input2' placeholder = '' readonly = 'readonly' type = 'text' value = '淘宝工单号 : ' >";
			$group_list .= "<input class = 'li-input33' placeholder = '' id = 'g_taobao_ance' type = 'text' value = {$g_taobao_ance}>";
			$group_list .= "</li>";

			$group_list .= "<li data-id = '82400'>";
			$group_list .= "<input class = 'li-input2' placeholder = '' readonly = 'readonly' type = 'text' value = '拼多多工单号 : ' >";
			$group_list .= "<input class = 'li-input33' placeholder = '' id = 'g_pdd_ance' type = 'text' value = {$g_pdd_ance}>";
			$group_list .= "</li>";

			$group_list .= "<li data-id = '82400'>";
			$group_list .= "<input class = 'li-input2' placeholder = '' readonly = 'readonly' type = 'text' value = '京东工单号 : ' >";
			$group_list .= "<input class = 'li-input33' placeholder = '' id = 'g_jd_ance' type = 'text' value = {$g_jd_ance}>";
			$group_list .= "</li>";

			$group_list .= "<li data-id = '82400'>";
			$group_list .= "<input class = 'li-input2' placeholder = '' readonly = 'readonly' type = 'text' value = '发单开始时间 : ' >";
			$group_list .= "<input class = 'li-input5' placeholder = '' id = 'g_bot_begin_work' type = 'time' value = {$g_start_time}>";
			$group_list .= "</li>";

			$group_list .= "<li data-id = '82400'>";
			$group_list .= "<input class = 'li-input2' placeholder = '' readonly = 'readonly' type = 'text' value = '发单结束时间 : ' >";
			$group_list .= "<input class = 'li-input5' placeholder = '' id = 'g_bot_end_work' type = 'time'  value = {$g_finish_time}>";
			$group_list .= "</li>";

			$group_list .= "<li data-id = '82400'>";
			$group_list .= "<input class = 'li-input2' placeholder = '' readonly = 'readonly' type = 'text' value = '发单状态 : ' >";
			$group_list .= "<select class = 'li-select' id = 'g_bot_statue' name = '发单状态'>";
			if ($g_bot_statue == 1) {
				$group_list .= "<option value='0'>暂停</option>";
				$group_list .= "<option value='1' selected='selected'>发单</option>";
			} else {
				$group_list .= "<option value='0' selected='selected'>暂停</option>";
				$group_list .= "<option value='1'>发单</option>";
			}
			$group_list .= "</select>";
			$group_list .= "</li>";

			$group_list .= "<li data-id = '82400'>";
			$group_list .= "<input class = 'li-input2' placeholder = '' readonly = 'readonly' type = 'text' value = '优选库 : ' >";
			$group_list .= "<select class = 'li-select' id = 'g_with_preferred' name = '优选库'>";
			if ($g_with_preferred == 1) {
				$group_list .= "<option value='0'>不选</option>";
				$group_list .= "<option value='1' selected='selected'>选中</option>";
			} else {
				$group_list .= "<option value='0' selected='selected'>不选</option>";
				$group_list .= "<option value='1'>选中</option>";
			}
			$group_list .= "</select>";
			$group_list .= "</li>";

			$group_list .= "<li data-id = '82400'>";
			$group_list .= "<input class = 'li-input2' placeholder = '' readonly = 'readonly' type = 'text' value = '发单间隔 : ' >";
			$group_list .= "<select class = 'li-select' id = 'g_interval' name = '发单间隔'>";
			$group_list .= get_wait_list($g_interval, 30, 24*60);
			$group_list .= "</select>";
			$group_list .= "</li>";

			$group_list .= "<li data-id = '82400'>";
			$group_list .= "<input class ='li-input2' placeholder = '' readonly = 'readonly' type = 'text' value = '午休间隔 : ' >";
			$group_list .= "<select class = 'li-select' id = 'g_lunch_break' name = '午休间隔'>";
			$group_list .= get_wait_list($g_lunch_break, 10, 5*60);
			$group_list .= "</select>";
			$group_list .= "</li>";

			// 获取商品列表
			$group_list .= get_good_list(explode(',', $parent_category_ids));

			$group_list .= "<li data-id='82400'>";
			$group_list .= "<button type = 'button' class = 'li-button' onClick = 'set_group_data()'>确定</button>";
			$group_list .= "</li>";
		}
		
		echo $group_list;
	}

	// 修改数据库数据
	function set_group_data () {
		$g_serial = $_COOKIE['g_serial'];
		if (!empty($g_serial)) {
			$g_bot_begin_work = $_POST['g_bot_begin_work'];
			$g_bot_end_work = $_POST['g_bot_end_work'];
			$g_bot_statue = $_POST['g_bot_statue'];
			$g_interval = $_POST['g_interval'];
			$g_good_type = $_POST['g_good_type'];
			$g_taobao_ance = $_POST['g_taobao_ance'];
			$g_pdd_ance = $_POST['g_pdd_ance'];
			$g_jd_ance = $_POST['g_jd_ance'];
			$g_lunch_break = $_POST['g_lunch_break'];
			$g_with_preferred = $_POST['g_with_preferred'];
			simQuery("UPDATE wechat_group SET taobao_ance = '{$g_taobao_ance}', pdd_ance = '{$g_pdd_ance}', jd_ance = '{$g_jd_ance}', start_time = '{$g_bot_begin_work}', finish_time = '{$g_bot_end_work}', status = '{$g_bot_statue}', `interval` = '{$g_interval}', with_preferred = '{$g_with_preferred}', lunch_break = '{$g_lunch_break}', parent_category_ids = '{$g_good_type}' WHERE id = {$g_serial}");
			echo 2;
		} else {
			echo 1;
		}
	}

}

/* End of file democontroller.php */